-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathTask.vb
355 lines (337 loc) · 15.5 KB
/
Task.vb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
Namespace My
''' <summary>
''' 进程管理相关函数
''' </summary>
''' <remarks></remarks>
Partial Public NotInheritable Class Task
''' <summary>
''' 运行程序(同步阻塞,TaskName程序会获得鼠标焦点,直到TaskName程序结束才继续向下执行)
''' </summary>
''' <param name="TaskName">程序名称(例如"notepad"或"notepad.exe")</param>
''' <returns>是否执行成功</returns>
''' <remarks></remarks>
Public Shared Function Run(ByVal TaskName As String) As Boolean
Try
If TaskName.ToLower.EndsWith(".exe") = False Then
TaskName = TaskName & ".exe"
End If
Dim p As New Process
p.StartInfo.FileName = "cmd.exe"
p.StartInfo.UseShellExecute = False
p.StartInfo.RedirectStandardInput = True
p.StartInfo.RedirectStandardOutput = True
p.StartInfo.RedirectStandardError = True
p.StartInfo.CreateNoWindow = True
p.Start()
p.StandardInput.WriteLine("start """""""" " & """" & TaskName & """" & " >nul 2>nul")
p.StandardInput.WriteLine("exit")
p.StandardOutput.ReadToEnd()
p.StandardOutput.Close()
p.Dispose()
Return True
Catch ex As Exception
Return False
End Try
End Function
''' <summary>
''' 运行程序(异步执行,多次调用本函数会打开多个TaskName程序)
''' </summary>
''' <param name="TaskName">程序名称(例如"notepad"或"notepad.exe")</param>
''' <param name="MouseFocus">TaskName程序是否获得鼠标焦点(程序运行后,有些可能会遮挡焦点窗体,如“记事本”、“画图”,有些甚至会强制抢占焦点,如“计算器”)</param>
''' <returns>是否执行成功</returns>
''' <remarks></remarks>
Public Shared Function RunAsync(ByVal TaskName As String, Optional ByVal MouseFocus As Boolean = True) As Boolean
Try
If TaskName.ToLower.EndsWith(".exe") = False Then
TaskName = TaskName & ".exe"
End If
If MouseFocus Then
Shell(TaskName, AppWinStyle.NormalFocus, False)
Else
Shell(TaskName, AppWinStyle.NormalNoFocus, False)
End If
Return True
Catch ex As Exception
Return False
End Try
End Function
''' <summary>
''' 关闭程序(同步阻塞,强制结束所有的TaskName程序的进程树)
''' </summary>
''' <param name="TaskName">程序名称(例如"notepad"或"notepad.exe")</param>
''' <returns>是否执行成功</returns>
''' <remarks></remarks>
Public Shared Function Kill(ByVal TaskName As String) As Boolean
Try
If TaskName.ToLower.EndsWith(".exe") = False Then
TaskName = TaskName & ".exe"
End If
Dim p As New Process
p.StartInfo.FileName = "cmd.exe"
p.StartInfo.UseShellExecute = False
p.StartInfo.RedirectStandardInput = True
p.StartInfo.RedirectStandardOutput = True
p.StartInfo.RedirectStandardError = True
p.StartInfo.CreateNoWindow = True
p.Start()
p.StandardInput.WriteLine("taskkill /f /t /im " & """" & TaskName & """" & " >nul 2>nul")
p.StandardInput.WriteLine("exit")
p.StandardOutput.ReadToEnd()
p.StandardOutput.Close()
p.Dispose()
Return True
Catch ex As Exception
Return False
End Try
End Function
''' <summary>
''' 关闭程序(异步执行,强制结束所有的TaskName程序的进程树)
''' </summary>
''' <param name="TaskName">程序名称(例如"notepad"或"notepad.exe")</param>
''' <returns>是否执行成功</returns>
''' <remarks></remarks>
Public Shared Function KillAsync(ByVal TaskName As String) As Boolean
Try
If TaskName.ToLower.EndsWith(".exe") = False Then
TaskName = TaskName & ".exe"
End If
Shell("taskkill /f /t /im " & """" & TaskName & """", AppWinStyle.Hide, False)
Return True
Catch ex As Exception
Return False
End Try
End Function
''' <summary>
''' 获取进程名称列表(全部进程,包括"Idle"进程,返回结果的字符串中不含".exe"后缀)
''' </summary>
''' <returns>结果字符串数组(失败返回空String数组)</returns>
''' <remarks></remarks>
Public Shared Function ListName() As String()
Dim Processes As Process() = Process.GetProcesses()
Dim TaskList As List(Of String) = New List(Of String)(Processes.Length)
For Each P In Processes
TaskList.Add(P.ProcessName)
Next
Return TaskList.ToArray()
End Function
''' <summary>
''' 获取窗口标题列表(有窗体的进程,只能获取到进程的主窗体的标题)
''' </summary>
''' <returns>结果字符串数组(失败返回空String数组)</returns>
''' <remarks></remarks>
Public Shared Function ListTitle() As String()
Dim Processes As Process() = Process.GetProcesses()
Dim TaskList As List(Of String) = New List(Of String)(Processes.Length)
For Each P In Processes
If P.MainWindowTitle <> "" Then
TaskList.Add(P.MainWindowTitle)
End If
Next
Return TaskList.ToArray()
End Function
''' <summary>
''' 获取进程文件路径列表(部分进程获取不到)
''' </summary>
''' <returns>结果字符串数组(失败返回空String数组)</returns>
''' <remarks></remarks>
Public Shared Function ListFilePath() As String()
Dim Processes As Process() = Process.GetProcesses()
Dim TaskList As List(Of String) = New List(Of String)(Processes.Length)
For Each P In Processes
Try
TaskList.Add(P.MainModule.FileName)
Catch ex As Exception
'MsgBox(P.ProcessName & ex.ToString())
End Try
Next
Return TaskList.ToArray()
End Function
''' <summary>
''' 根据进程名称,获取进程
''' </summary>
''' <param name="TaskName">进程名称(不含".exe"后缀)</param>
''' <returns>结果进程(Process,失败返回Nothing)</returns>
''' <remarks></remarks>
Public Shared Function FindByName(ByVal TaskName As String) As Process
Dim Processes As Process() = Process.GetProcesses()
For Each P In Processes
If P.ProcessName.ToLower() = TaskName.ToLower() Then
Return P
End If
Next
Return Nothing
End Function
''' <summary>
''' 根据进程文件路径,获取进程
''' </summary>
''' <param name="FilePath">文件路径(可以是相对路径)</param>
''' <returns>结果进程(Process,失败返回Nothing)</returns>
''' <remarks></remarks>
Public Shared Function FindByFilePath(ByVal FilePath As String) As Process
Dim Processes As Process() = Process.GetProcesses()
If FilePath.Contains(":") = False Then
FilePath = System.IO.Directory.GetCurrentDirectory + "\" + FilePath
End If
For Each P In Processes
Try
If P.MainModule.FileName.ToLower() = FilePath.ToLower() Then
Return P
End If
Catch ex As Exception
'MsgBox(P.ProcessName & ex.ToString())
End Try
Next
Return Nothing
End Function
''' <summary>
''' 搜索窗口标题,获取进程(优先搜索字符串完全相同的,然后搜索包含有指定字符串的)
''' </summary>
''' <param name="Title">窗口标题(字符串不必完全相同)</param>
''' <returns>结果进程(Process,失败返回Nothing)</returns>
''' <remarks></remarks>
Public Shared Function SearchByTitle(ByVal Title As String) As Process
Dim Processes As Process() = Process.GetProcesses()
For Each P In Processes
If P.MainWindowTitle.ToLower() = Title.ToLower() Then
Return P
End If
Next
For Each P In Processes
If P.MainWindowTitle.ToLower().Contains(Title.ToLower()) Then
Return P
End If
Next
Return Nothing
End Function
Private Declare Function SuspendThread Lib "kernel32.dll" Alias "SuspendThread" (ByVal hThread As IntPtr) As Int32
Private Declare Function ResumeThread Lib "kernel32.dll" Alias "ResumeThread" (ByVal hThread As IntPtr) As Int32
Private Declare Function OpenThread Lib "kernel32.dll" Alias "OpenThread" (ByVal dwDesiredAccess As UInt32, ByVal bInheritHandle As Boolean, ByVal dwThreadId As UInt32) As IntPtr
<Flags()> _
Private Enum ThreadAccess As UInt32
Standard = &HF0000UI
Synchronize = &H100000UI
All = &H1F0FFFUI
End Enum
''' <summary>
''' 挂起进程的所有线程(Suspend Count加1)
''' </summary>
''' <param name="Process">进程(Process)</param>
''' <returns>是否执行成功</returns>
''' <remarks></remarks>
Public Shared Function ThreadSuspend(ByVal Process As Process) As Boolean
If Process Is Nothing OrElse Process.HasExited Then
Return False
End If
Dim Threads As ProcessThreadCollection = Process.Threads
Dim Result As Boolean = True
For I = 0 To Threads.Count - 1
Dim ThreadId As Int32 = Threads(I).Id
Dim ThreadhWnd As IntPtr = OpenThread(ThreadAccess.All, False, ThreadId)
Result = Result And SuspendThread(ThreadhWnd) <> -1
Next
Return Result
End Function
''' <summary>
''' 恢复进程的所有线程(Suspend Count减1)
''' </summary>
''' <param name="Process">进程(Process)</param>
''' <returns>是否执行成功</returns>
''' <remarks></remarks>
Public Shared Function ThreadResume(ByVal Process As Process) As Boolean
If Process Is Nothing OrElse Process.HasExited Then
Return False
End If
Dim Threads As ProcessThreadCollection = Process.Threads
Dim Result As Boolean = True
For I = 0 To Threads.Count - 1
Dim ThreadId As Int32 = Threads(I).Id
Dim ThreadhWnd As IntPtr = OpenThread(ThreadAccess.All, False, ThreadId)
Result = Result And ResumeThread(ThreadhWnd) <> -1
Next
Return Result
End Function
''' <summary>
''' 强制恢复进程的所有线程(Suspend Count减到0)
''' </summary>
''' <param name="Process">进程(Process)</param>
''' <returns>是否执行成功</returns>
''' <remarks></remarks>
Public Shared Function ThreadFree(ByVal Process As Process) As Boolean
If Process Is Nothing OrElse Process.HasExited Then
Return False
End If
Dim Threads As ProcessThreadCollection = Process.Threads
Dim Result As Boolean = True
For I = 0 To Threads.Count - 1
Dim ThreadId As Int32 = Threads(I).Id
Dim ThreadhWnd As IntPtr = OpenThread(ThreadAccess.All, False, ThreadId)
Dim Temp As Int32 = ResumeThread(ThreadhWnd)
Result = Result And Temp <> -1
While Temp > 0
Temp = ResumeThread(ThreadhWnd)
Result = Result And Temp <> -1
End While
Next
Return Result
End Function
''' <summary>
''' 限制进程的CPU占用(通过不断挂起和恢复线程)
''' </summary>
''' <param name="Process">进程(Process)</param>
''' <param name="SleepMillisecond">挂起等待的持续时间(默认50毫秒,最少1毫秒)</param>
''' <param name="IntervalMillisecond">挂起恢复的间隔时间(默认50毫秒,最少1毫秒)</param>
''' <returns>是否执行成功</returns>
''' <remarks></remarks>
Public Shared Function ThreadLimit(ByVal Process As Process, Optional ByVal SleepMillisecond As UInt32 = 50, Optional ByVal IntervalMillisecond As UInt32 = 50) As Boolean
If Process Is Nothing OrElse Process.HasExited Then
Return False
End If
If SleepMillisecond <= 0 Then
SleepMillisecond = 1
End If
If IntervalMillisecond <= 0 Then
IntervalMillisecond = 1
End If
Dim Temp As New ThreadLimitTask(Process, SleepMillisecond, IntervalMillisecond)
Return True
End Function
Private Class ThreadLimitTask
Private Process As Process
Private SleepMillisecond As UInt32
Private IntervalMillisecond As UInt32
Private Thread As New Threading.Thread(New Threading.ThreadStart(AddressOf Run))
Public Sub New(ByVal TaskProcess As Process, ByVal TaskSleepMillisecond As UInt32, ByVal TaskIntervalMillisecond As UInt32)
Process = TaskProcess
SleepMillisecond = TaskSleepMillisecond
IntervalMillisecond = TaskIntervalMillisecond
Thread.Start()
End Sub
Private Sub Run()
While Thread.IsAlive
If Process.HasExited Then
Thread.Abort()
End If
Dim Threads As ProcessThreadCollection = Process.Threads
For I = 0 To Threads.Count - 1
Dim ThreadId As Int32 = Threads(I).Id
Dim ThreadhWnd As IntPtr = OpenThread(ThreadAccess.All, False, ThreadId)
SuspendThread(ThreadhWnd)
Next
Try
System.Threading.Thread.Sleep(SleepMillisecond)
Catch ex As Exception
End Try
For I = 0 To Threads.Count - 1
Dim ThreadId As Int32 = Threads(I).Id
Dim ThreadhWnd As IntPtr = OpenThread(ThreadAccess.All, False, ThreadId)
ResumeThread(ThreadhWnd)
Next
Try
System.Threading.Thread.Sleep(IntervalMillisecond)
Catch ex As Exception
End Try
End While
End Sub
End Class
End Class
End Namespace